package cn.mes.module.mes.service.basetimeperiod;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.mes.module.mes.controller.admin.basetimeperiod.vo.*;
import cn.mes.module.mes.dal.dataobject.basetimeperiod.BaseTimePeriodDO;
import cn.mes.framework.common.pojo.PageResult;
import cn.mes.framework.common.pojo.PageParam;
import cn.mes.framework.common.util.object.BeanUtils;

import cn.mes.module.mes.dal.mysql.basetimeperiod.BaseTimePeriodMapper;

import static cn.mes.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.mes.module.mes.enums.ErrorCodeConstants.*;

/**
 * 时间段配置信息 Service 实现类
 *
 * @author 游创
 */
@Service
@Validated
public class BaseTimePeriodServiceImpl implements BaseTimePeriodService {

    @Resource
    private BaseTimePeriodMapper baseTimePeriodMapper;

    @Override
    public String createBaseTimePeriod(BaseTimePeriodSaveReqVO createReqVO) {
        // 插入
        BaseTimePeriodDO baseTimePeriod = BeanUtils.toBean(createReqVO, BaseTimePeriodDO.class);
        baseTimePeriodMapper.insert(baseTimePeriod);
        // 返回
        return baseTimePeriod.getId();
    }

    @Override
    public void updateBaseTimePeriod(BaseTimePeriodSaveReqVO updateReqVO) {
        // 校验存在
        validateBaseTimePeriodExists(updateReqVO.getId());
        // 更新
        BaseTimePeriodDO updateObj = BeanUtils.toBean(updateReqVO, BaseTimePeriodDO.class);
        baseTimePeriodMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseTimePeriod(String id) {
        // 校验存在
        validateBaseTimePeriodExists(id);
        // 删除
        baseTimePeriodMapper.deleteById(id);
    }

    private void validateBaseTimePeriodExists(String id) {
        if (baseTimePeriodMapper.selectById(id) == null) {
            throw exception(BASE_TIME_PERIOD_NOT_EXISTS);
        }
    }

    @Override
    public BaseTimePeriodDO getBaseTimePeriod(String id) {
        return baseTimePeriodMapper.selectById(id);
    }

    @Override
    public PageResult<BaseTimePeriodDO> getBaseTimePeriodPage(BaseTimePeriodPageReqVO pageReqVO) {
        return baseTimePeriodMapper.selectPage(pageReqVO);
    }

}